// 
// ResourceManager.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Linq;
using System.Collections.Generic;
using Mono.Addins;
using Common.Resources.Items;
using Common.Resources.ExtensionNodes;
using System.Drawing;

namespace Common.Resources
{
	public class ResourceManager : IDisposable
	{
		IDictionary<string, ResourceItem> resources;
		
		private static ResourceManager instance;
		
		public static ResourceManager Instance
		{
			get {
				if (instance == null)
					instance = new ResourceManager ();
				return instance;
			}
		}
		
		private ResourceManager ()
		{
			this.LoadResources();
		}
		
		private void LoadResources ()
		{
			this.resources = new Dictionary<string, ResourceItem> ();
			
			var iconItems = AddinManager.GetExtensionNodes<AbstractResourceNode>("/Common/Resources/IconResource");
			var imageItems = AddinManager.GetExtensionNodes<AbstractResourceNode>("/Common/Resources/ImageResource");
			var items = new List<AbstractResourceNode>();
			
			items.AddRange(iconItems);
			items.AddRange(imageItems);
			
			Logging.Logger.AddInfo("ResourceManager: Found " + items.Count + " resource items.");
			
			foreach (var item in items) 
			{
				this.resources.Add(item.Id, item.GetResourceItem());
			}
		}
		
		public Image GetImage (string id)
		{
			Image image = null;
			
			if (!this.resources.ContainsKey (id))
				Logging.Logger.AddWarning ("Resource id " + id + " wasn't found");
			else {
				var res = this.resources[id];
				if (res != null && res is ImageResourceItem)
					image = (res as ImageResourceItem).GetImage ();
				else
					Logging.Logger.AddWarning ("No valid resource for id " + id + " - icon expected!");
			}
			
			return image;
		}
		
		public Icon GetIcon (string id)
		{
			Icon icon = null;
			if (!this.resources.ContainsKey (id))
				Logging.Logger.AddWarning ("Resource id " + id + " wasn't found");
			else
			{
				var res = this.resources[id];
				if (res != null && res is IconResourceItem)
					icon = (res as IconResourceItem).GetIcon ();
				else
					Logging.Logger.AddWarning("No valid resource for id " + id + " - icon expected!");
			}
			
			return icon;
		}
		
		public string GetLocalizedString (string id, string localeCode)
		{
			throw new NotImplementedException();
		}
		
		#region IDisposable implementation
		public void Dispose ()
		{
			if (this.resources != null)
				foreach (var res in this.resources) 
				{
					if (res.Value != null)
						res.Value.Dispose();
				}
		}
		#endregion
	}
}

